define(function(require, exports, module){
	"require:nomunge,exports:nomunge,module:nomunge";
	//异步请求
var wind_async=require('ddf_wind_async');
	
var base=require('component-base');

var history = require('component-history');
	
require('component-page');

	var beginDate, endDate, operType, beginMoney, endMoney, keyRange, keyword_s, queryDate,orderId;

	
	function initParameter() {
		beginDate = ""; // 开始时间
		endDate = ""; // 结束时间
		operType = ""; // 操作类型
		beginMoney = ""; // 最小金额
		endMoney = ""; // 最大金额
		keyRange = "";
		keyword_s = "";
		queryDate = "";
		orderId = "";
	}

	/**
	 * 查询代理商列表
	 */
	exports.init=function(m){
	    window.$ddf_account = m;
	    history.addHis(this,arguments);
		initParameter();

		var htmlStr = "";

		htmlStr += '<div id="content-header"><div id="breadcrumb"> <a href=javascript:load("'
				+ ddf.index.index_js
				+ '") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:void(0)" class="current">账户流水</a></div></div>';
		htmlStr += '<div class="container-fluid">';
//		htmlStr += '<hr/>';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
		htmlStr += '<div class="widget-box"><div class="widget-title"><span class="icon"><i class="icon-money"></i></span><h5>账户</h5></div>';
		htmlStr += '<div class="widget-content clearfix"><div class="fontsize"><span>备付金账户余额：<span class="color_f90" id="accountBalance"></span>元</span></div></div>';
		htmlStr += '<div id="alert-tips0" ></div>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"><span class="icon"><i class="icon-search"></i></span><h5>搜索条件</h5><button type="button" class="label label-success toggle">缩放</button></div>  ';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<form class="form-horizontal">';
		htmlStr += '<div class="control-group ">';
		htmlStr += '<label class="control-label">创建时间：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span2 m-wrap Wdate" type="text" id="beginDate" onClick=WdatePicker({dateFmt:"yyyy-MM-dd",readOnly:false}) />&nbsp;&nbsp;至&nbsp;&nbsp;<input class="span2 m-wrap Wdate" type="text" id="endDate" onClick=WdatePicker({dateFmt:"yyyy-MM-dd",readOnly:false}) />&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_0" onclick=$ddf_account.toQueryDate(0) class="btn btn-primary">所有</button>&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_1" onclick=$ddf_account.toQueryDate(1) class="btn">今天</button>&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_2" onclick=$ddf_account.toQueryDate(2) class="btn">最近1个月</button>&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_3" onclick=$ddf_account.toQueryDate(3) class="btn">3个月</button>&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_4" onclick=$ddf_account.toQueryDate(4) class="btn">1年</button>&nbsp;&nbsp;';
		htmlStr += '<button type="button" id="queryDate_5" onclick=$ddf_account.toQueryDate(5) class="btn">1年前</button>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="control-group"><label class="control-label">流水类型：</label><div class="controls"><div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle"><span id="operCodeListName">所有</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu" id="operCodeList"><li><a href="####">请选择</a></li><li><a href="####">网站订单</a></li><li><a href="####">手机订单</a></li><li><a href="####">终端订单</a></li></ul></div></div></div>';
		htmlStr += '<div class="control-group " id = "control-money">';
		htmlStr += '<label class="control-label">金额范围：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span2 m-wrap" type="text" id="beginMoney"/>&nbsp;&nbsp;至&nbsp;&nbsp;<input class="span2 m-wrap" type="text" id="endMoney"/>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group"><label class="control-label">关键词：</label><div class="controls"><div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle"><span id="keyRangeName">所有</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu" id="keyRangeList"><li><a href="####">请选择</a></li><li><a href="####">网站订单</a></li><li><a href="####">手机订单</a></li><li><a href="####">终端订单</a></li></ul></div>&nbsp;&nbsp;&nbsp;<input class="span3 m-wrap" id="keyword_s" type="text" /></div></div>';
		
		htmlStr += '<div class="row-fluid">';
    	htmlStr += '<div class="span6">';
    	htmlStr += '<div class="control-group">';
    	htmlStr += '<label class="control-label">订单编号：</label>';
    	htmlStr += '<div class="controls">';
    	htmlStr += '<input class="span8 m-wrap" type="text" id="orderId"/>';
    	htmlStr += '</div>';
    	htmlStr += '</div>';
    	htmlStr += '</div>';
    	htmlStr += '</div> ';
		
		htmlStr += '<div class="form-actions">';
		htmlStr += '<button class="btn btn-primary" type="button" onClick=$ddf_account.searchAgent()>搜索</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		htmlStr += '<button class="btn btn-info" type="button" onClick=$ddf_account.resetSearchParams()>重置</button>';
		htmlStr += '</div>';
		htmlStr += '</form>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		// -------------------------------------
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>流水列表</h5></div>';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered data-table"><thead>';
		htmlStr += '<tr><th>序号</th><th>创建时间</th><th>流水单号</th><th>订单号</th><th>流水类型</th><th>金额 /元</th><th>操作</th></tr></thead>';

		htmlStr += '<tbody id="accountList">';
		htmlStr += '</tbody>';
		htmlStr += '</table>';

		htmlStr += '<div id="footPageDiv0">';

		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div> ';

		showDiv("content0");
		$("#content0").html(htmlStr);

		
		getAccountDetails(1,pageSize, beginDate, endDate, beginMoney, endMoney,
				operType, keyRange, keyword_s,orderId);
		getAccountType();
		getkeyRange();
		
		$("#beginMoney").change(function() {
			 var bgM = $("#beginMoney").val();
		     if(!isNumber2Decimal(bgM)){
		    	 addErrorMessage("control-money", "endMoney", "账户金额为数字类型");
		     }else{
		    	 clearErrorMessage("control-money");
		     }
		});
		
		$("#endMoney").change(function(){
			var edM = $("#endMoney").val();
			if(!isNumber2Decimal(edM)){
				addErrorMessage("control-money","endMoney","账户金额为数字类型");
			}else{
				clearErrorMessage("control-money");
			}
		});
	}

	/**
	 * 获取账户详细表单
	 */
	function getAccountDetails(pageNumber,pageSize, beginDate, endDate, beginMoney,
			endMoney, operType, keyRange, keyword_s,orderId) {
		var params = {
			"page.pageNumber" : pageNumber,
			"page.pageSize" : pageSize,
			"queryDate" : queryDate,
			"beginDate" : beginDate,
			"endDate" : endDate,
			"beginMoney" : beginMoney,
			"endMoney" : endMoney,
			"operType" : operType,
			"keyRange" : keyRange,
			"orderId" : orderId,
			"keyword_s" : keyword_s
		};

		base.showLoading();

		eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.account.getAccountDetails,params));
			
			getAccountDetailsCallBack(data);
			
		}))().start();
	}

	/**
	 * 回调函数
	 * 
	 * @param data
	 */
	function getAccountDetailsCallBack(data) {
		var htmlStr = "";
		if(data["code"] ==1){
		for (var i = 0; i < data["resultList"].length; i++) {
			htmlStr += '<tr>';
			htmlStr += '<td>' + getSequence(data["pageNumber"], i + 1)
					+ '</td>';
			htmlStr += '<td>' + data["resultList"][i]["createTime"] + '</td>';
			htmlStr += '<td>' + data["resultList"][i]["accountJourId"]+ '</td>';
			htmlStr += '<td>' + data["resultList"][i]["orderId"]+ '</td>';
			htmlStr += '<td>' + data["resultList"][i]["operName"] + '</td>';
			htmlStr += '<td>' + parseFloat(data["resultList"][i]["amount"]) + '</td>';
			if(data["resultList"][i]["operCode"]==4){
				htmlStr += '<td><a href=javascript:$ddf_account.loadAccDetail("'+data["resultList"][i]["accountJourId"]+'")>详情</a></td>';
			}else{
				htmlStr += '<td>----</td>';
			}
			    htmlStr += '</tr>';
		}
		$("#accountList").html(htmlStr);
		$("#accountBalance").html(parseFloat(data["balance"])); // 设置金额
		
		
		 $('#footPageDiv0').ddf_pagination(data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"],{  
			 
			 changePageCallback:function(pageSize,page){ 
				 getAccountDetails(page,pageSize, beginDate, endDate, beginMoney, endMoney,
							operType, keyRange, keyword_s,orderId);

			 },
			 changePageSizeCallBack:function(pageSize){
				 getAccountDetails(1,pageSize, beginDate, endDate, beginMoney, endMoney,
							operType, keyRange, keyword_s,orderId);
	     	 
			 } 
	     }); 
		
		
		}else{
			base.tips_error(data["message"]);
		}
		base.cancelLoading();
	  }
	
	/**
	 * 查看账户流水詳情信息
	 */
	var selectAccountJourId = ""; //選中的流水号
	function loadAccDetail(_selectAccountJourId){
		history.addHis(this,arguments);
		selectAccountJourId=_selectAccountJourId;
		var htmlStr="";
		
		htmlStr += '<div id="content-header">';
		htmlStr += '<div id="breadcrumb"> <a href=javascript:load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href=javascript:$ddf_account.showDiv("content0")>账户流水管理</a><a href="javascript:void(0)" class="current">账户流水详情</a></div>';
//		htmlStr += '<h1>账户流水详情</h1>';
		htmlStr += '</div>    ';

		htmlStr += '<div class="container-fluid">';
//		htmlStr += '<hr/>';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
		htmlStr += '<div id="alert-tips1" ></div>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-reorder"></i></span>';
		htmlStr += '<h5>操作</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content">';
		htmlStr += ' <button type="button" class="btn btn-info" onClick=$ddf_account.showDiv("content0")>返回</button>';
		htmlStr += '</div>            ';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>交易跟踪</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content">';
		htmlStr += '<p><span>提现申请状态：</span><span id="applystautusname"></span></p>';
		htmlStr += '<p><span>订单处理状态：</span><span id="settleresult"></span></p>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<hr>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>订单申请信息</h5>';
		htmlStr += '</div>        ';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered data-table">';
		htmlStr += '<tbody>';
		htmlStr += '<tr><td><span>流水单号：</span><span id="show_accountJourId"></span></td><td><span>账户代码：</span><span id="accountId"></span></td><td><span>申请时间：</span><span id="applyCommitTime"></span></td></tr>';
		htmlStr += '<tr><td><span>提现金额：</span><span id="amount"></span></td><td><span><span>手续费：</span><span id="fee"></span></td><td><span>申请方式：</span><span id="applyTypeName"></span></td></tr>';
		htmlStr += '</tbody>';
		htmlStr += '</table>  	';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>订单处理信息</h5>';
		htmlStr += '</div>        ';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered data-table">';
		htmlStr += '<tbody>';
		htmlStr += '<tr><td><span>开始时间：</span><span id="reqBankTime"></span></td><td><span>结算类型：</span><span id="operationName"></span></td><td><span>完成时间：</span><span id="finishBankTime"></span></td></tr>';
		htmlStr += '</tbody>';
		htmlStr += '</table>  	';
		htmlStr += '</div>';
		htmlStr += '</div>  ';
		
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div>'; 
		showDiv("content1");
		
		$("#content1").html(htmlStr);
		
		var params={
					
					"accountJourId":selectAccountJourId
					
			        };
		
		base.showLoading();

		eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.account.getAccountDetailsById,params));
			
			loadAccDetailCallBack(data);
			
		}))().start();
	}
	
	/**
	 * 查看详情的回调函数
	 * @param data
	 */
	function loadAccDetailCallBack(data){
		var htmlStr="";
		if(data["code"]==1){
			if(data["applystautusId"]!=null){
				if(data["applystautusId"]=="2"){
					$("#applystautusname").html("<font color='00C618' size='2px'>"+data["applystautusname"]+"</font>");
				}else{
					$("#applystautusname").html("<font color='red' size='2px'>"+data["applystautusname"]+"</font>");
				}
			}else{
				$("#applystautusname").html("");
			}
			
			if(data["settleResultId"]!=null){
				if(data["settleResultId"]=="1"){
					$("#settleresult").html("<font color='00C618' size='2px'>"+data["settleresult"]+"</font>");
				}else{
					$("#settleresult").html("<font color='red' size='2px'>"+data["settleresult"]+"</font>");
				}
			}else{
				$("#settleresult").html("");
			}
			$("#show_accountJourId").html(data["accountJourId"]);
			$("#accountId").html(data["accountId"]);
			$("#applyCommitTime").html(data["applyCommitTime"]);
			$("#amount").html(data["amount"]);
			$("#fee").html(data["fee"]);
			$("#applyTypeName").html(data["applyTypeName"]);
			$("#reqBankTime").html(data["reqBankTime"]);
			$("#operationName").html(data["operationName"]);
			$("#finishBankTime").html(data["finishBankTime"]);
		}else{
			base.tips_error(data["message"]);
		}
		base.cancelLoading();

	}

	/**
	 * 账户类型
	 */
	function getAccountType() {

		eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.account.getAccountType,""));
			
			getAccountTypeCallBack(data);
			
		}))().start();

	}

	/*
	 * 账户类型的回调函数
	 */
	function getAccountTypeCallBack(data) {
		htmlStr = "";
		if(data["code"] == 1){
		for (var i = 0; i < data["resultOperCodeList"].length; i++) {
			htmlStr += '<li><a href=javascript:$ddf_account.setoperCodeName("'
					+ data["resultOperCodeList"][i]["operationCode"] + '","'
					+ data["resultOperCodeList"][i]["operation"] + '")>'
					+ data["resultOperCodeList"][i]["operation"] + '</a></li>';
		}
		$("#operCodeList").html(
				'<li><a href=javascript:$ddf_account.setoperCodeName("","所有")>所有</a></li>'
						+ htmlStr);
		}
	}

	/**
	 * 选中并设置值
	 * 
	 * @param _operCode
	 * @param operName
	 */
	function setoperCodeName(_operCode, operName) {
		operType = _operCode;
		$("#operCodeListName").html(operName);
	}

	/**
	 * 获取关键值
	 */
	function getkeyRange() {
		bill = "bill", name = "name", billV = "流水单号", nameV = "流水类型";
		htmlStr = "";
		htmlStr += '<li><a href=javascript:$ddf_account.setKeyWordsName("'
				+ bill + '","' + billV + '")>' + billV + '</a></li>';
		htmlStr += '<li><a href=javascript:$ddf_account.setKeyWordsName("'
				+ name + '","' + nameV + '")>' + nameV + '</a></li>';
		$("#keyRangeList").html(
				'<li><a href=javascript:$ddf_account.setKeyWordsName("","所有")>所有</a></li>'
						+ htmlStr);
	}

	function setKeyWordsName(_keyRange, keyRangeName) {
		// alert("value=" + _keyRange);
		keyRange = _keyRange;
		$("#keyRangeName").html(keyRangeName);

	}

	/**
	 * 显示DIV
	 */
	function showDiv(block){
		
		$("#content > div").hide();
		
		if($("#"+block).length<=0){
			
			$("#content").append('<div id="'+block+'"></div>'); 
			
		}else{
			
			$("#"+block).show();
		}
		
		if(block=="content0"){
			shopTypeId="";
		}
		

		
	}

	

	/**
	 * 搜索
	 */
	function searchAgent() {
		history.addHis(this,arguments);
		beginDate = $("#beginDate").val();
		endDate = $("#endDate").val();
		beginMoney = $("#beginMoney").val();
		endMoney = $("#endMoney").val();
		keyword_s = $("#keyword_s").val();
		orderId = $("#orderId").val();
		if (parseFloat(beginMoney) > parseFloat(endMoney)) {
			base.tips_error("开始金额不能大于结束金额");
			return;
		}
		if(!base.checkDate(beginDate, endDate)){
			return;
		} // 日期验证
		getAccountDetails(1,pageSize, beginDate, endDate, beginMoney, endMoney,
				operType, keyRange, keyword_s,orderId);
	}

	/**
	 * 重置参数
	 */
	function resetSearchParams() {
		beginDate = "";
		endDate = "";
		beginMoney = "";
		endMoney = "";
		queryDate="";
		orderId="";
		$("#beginDate").val("");

		$("#endDate").val("");

		$("#beginMoney").val("");

		$("#endMoney").val("");

		$("#keyword_s").val("");
		$("#orderId").val("");

		setKeyWordsName("", "所有");

		setoperCodeName("", "所有");
		
		//设置固定日期的样式
		for (var i = 0; i < 6; i++) {
			$("#queryDate_" + i).removeClass("btn-primary"); 
		}
		
		$("#queryDate_0").addClass("btn-primary");
	}

	/**
	 * 每个时间的搜索
	 * 
	 * @param _value
	 */
	function toQueryDate(_value) {
		queryDate = _value;
		for (var i = 0; i < 6; i++) {
			$("#queryDate_" + i).removeClass("btn-primary"); // 自选日期格式查询,移除固定日期链接样式
		}
		$("#queryDate_" + queryDate).addClass("btn-primary");
	}

	
	exports.getAccountDetails = getAccountDetails; // 获取账户信息
	exports.getAccountType = getAccountType; // 获取账户类型
	exports.setoperCodeName = setoperCodeName; // 设置操作
	exports.getkeyRange = getkeyRange; // 获取搜索的范围
	exports.setKeyWordsName = setKeyWordsName; // 设置搜索的关键字
	exports.searchAgent = searchAgent; // 搜索
	exports.resetSearchParams = resetSearchParams; // 重置参数
	exports.toQueryDate = toQueryDate; // 时间样式的改变
	exports.loadAccDetail= loadAccDetail; //查看詳情
	exports.showDiv = showDiv; //查看详情的回调函数
});